package com.jt.common.Thread;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class SyncTests {

    //private static List<String> cache=new Vector<>();
    //CopyOnWriteArrayList是一个线程安全的List集合,允许多个线程执行并发更新,但是只能有一个更新成功.
    private static List<String> cache = new CopyOnWriteArrayList<>();//CAS(比较和交换)

    public static List<String> selectAll() {
        if (cache.isEmpty()) {
            synchronized (cache) {
                if (cache.isEmpty()) {
                    List<String> cates = Arrays.asList("a","b","c");
                    cache.addAll(cates);
                }
            }
        }
        return cache;
    }
}
